package com.company;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class NGuessNumber {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        String input;
        while (true) {
            int[] test = generateAnswer();
            while (true) {
                int a = compareGuessAnswer(test, getPlayerInput());
                if (a == 4) {
                    System.out.println("恭喜您猜对了！您获胜了");
                    break;
                } else {
                    System.out.printf("%dA%dB \n", a, (4 - a));
                }
            }
            System.out.println("继续游戏请输入y,输入n则退出游戏");
            while (true){
                input = scanner.next();
                if (input.equals("y")||input.equals("n")){
                    break;
                }else {
                    System.out.println("输入有误");
                }
            }
            if (input.equals("y")) {
                continue;
            } else {
                break;
            }
        }
    }
    public static int[] generateAnswer() {
        Random random = new Random();
        int[] guess = new int[4];
        int shu = random.nextInt(10);
        guess[0] = shu;
        for (int i = 0; i < guess.length; i++) {
            shu = random.nextInt(10);
            for (int num = 0; num < i; num++) {
                while (shu == guess[num]) {
                    shu = random.nextInt(10);
                    num = 0;
                }
            }
            guess[i] = shu;
        }
        for (int i = 0; i < guess.length; i++) {
            for (int j = i + 1; j < guess.length; j++) {
                if (guess[i] == guess[j]) {
                    guess[j] = (guess[j] * guess[j] + 1) % 10;
                }
            }
        }
        System.out.println(Arrays.toString(guess));

        return guess;
    }
    public static int[] getPlayerInput() {
        Scanner scanner = new Scanner(System.in);
        int[] number = new int[4];
        System.out.println("请输入四个不重复的数字");
        for (int i = 0; i < number.length; i++) {
            System.out.println("第" + (i + 1) + "个数");
            number[i] = scanner.nextInt();
        }
        return number;
    }
    public static int compareGuessAnswer(int[] guess, int[] number) {
        int a = 0, b = 0;
        for (int i = 0; i < guess.length; i++) {
            for (int k : number) {
                if (guess[i] == number[i]) {
                    a++;
                    break;
                }
                if (k == guess[i]) {
                    b++;
                }
            }
        }
        return a;
    }
    public static boolean isWin(int a) {
        boolean victory;
        if (a == 4) {
            victory = true;
        } else {
            victory = false;
        }
        return victory;
    }
}
